﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SolutionNavigateAddIn.Watcher
{
    public partial class StartupForm : Form
    {
        private Keys key1, key2;
        
        public StartupForm()
        {
            InitializeComponent();
        }
        
        public void InitSettings(SnSettings settings)
        {
            textBoxShortcut.Text = settings.GlobalKey;
            checkBoxTrace.Checked = settings.CanTrace;
        }

        private void textBoxShortcut_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                RemoveLastKey();
            }
            else
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                if (key1 == Keys.None)
                {
                    TryToSet(ref key1, e);
                }
                else
                {
                    TryToSet(ref key2, e);
                }
            }
            GenerateText();
        }

        private void TryToSet(ref Keys key, KeyEventArgs args)
        {
            if (args.KeyValue > 20)
            {
                key = args.KeyData;
            }
        }

        private void RemoveLastKey()
        {
            if (key2 != Keys.None)
            {
                key2 = Keys.None;
            }
            else if (key1 != Keys.None)
            {
                key1 = Keys.None;
            }
        }

        private void GenerateText()
        {
            string s = Keys2String(key2);

            s = (string.IsNullOrEmpty(s)) ? Keys2String(key1) : Keys2String(key1) + ", " + s;

            if (!string.IsNullOrEmpty(s))
                s = "Global::" + s;

            textBoxShortcut.Text = s;
        }

        private string Keys2String(Keys keys)
        {
            string s = string.Empty;
            if (keys != Keys.None)
            {
                Keys value = keys;
                if ((keys & Keys.Control)>0)
                {
                    s += "Ctrl";
                    value ^= Keys.Control;
                }
                if ((keys & Keys.Alt) > 0)
                {
                    s += (string.IsNullOrEmpty(s)) ? "Alt" : "+Alt";
                    value ^= Keys.Alt;
                }
                if ((keys & Keys.Shift) > 0)
                {
                    s += (string.IsNullOrEmpty(s)) ? "Shift" : "+Shift";
                    value ^= Keys.Shift;
                }

                if (value <= 0)
                {
                    return string.Empty;
                }

                s += (string.IsNullOrEmpty(s)) ? value.ToString() : "+" + value;
            }
            return s;
        }

        private void textBoxShortcut_TextChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox1.Items.AddRange(SnEnvironment.CheckShortcutUsage(textBoxShortcut.Text).ToArray());
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start("http://solutionnavigator.codeplex.com");
            }
            catch
            {
                
            }
            
        }

        public string GlobalKey
        {
            get
            {
                return textBoxShortcut.Text;
            }
        }

        public bool CanTrace
        {
            get
            {
                return checkBoxTrace.Checked;
            }
        }

        public bool ShowSettings
        {
            get
            {
                return checkBoxShowInStartup.Checked;
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxShortcut.Text = string.Empty;
            key1 = Keys.None;
            key2 = Keys.None;
        }
    }
}
